package com.catjiu.wangEditor.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;

import java.util.List;

@Data
public class DomText extends DomNode{
    private String text;
    private Boolean bold;
    private Boolean underline;
    private Boolean italic;
    private Boolean through;
    private Color color;
    private Color bgColor;
    private Integer fontSize;
    private String fontFamily;

    @Override
    public String getType() {
        return null;
    }

    public static DomText from(JSONObject obj){
        DomText domText = new DomText();
        domText.setText(obj.getString("text"));
        if(obj.containsKey("bold")){
            domText.setBold(obj.getBoolean("bold"));
        }
        if(obj.containsKey("underline")){
            domText.setUnderline(obj.getBoolean("underline"));
        }
        if(obj.containsKey("italic")){
            domText.setItalic(obj.getBoolean("italic"));
        }
        if(obj.containsKey("through")){
            domText.setThrough(obj.getBoolean("through"));
        }
        if(obj.containsKey("color")){
            domText.setColor(new Color(obj.getString("color")));
        }
        if(obj.containsKey("bgColor")){
            domText.setBgColor(new Color(obj.getString("bgColor")));
        }
        if(obj.containsKey("fontSize")){
            domText.setFontSize(Integer.parseInt(obj.getString("fontSize").replace("px","")));
        }
        if(obj.containsKey("fontFamily")){
            domText.setFontFamily(obj.getString("fontFamily"));
        }
        return domText;
    }

    public void render(XWPFRun run){
        run.setText(text);
        if(bold!=null && bold){
            run.setBold(bold);
        }
        if(underline!=null && underline){
            run.setUnderline(UnderlinePatterns.SINGLE);
        }
        if(italic!=null && italic){
            run.setItalic(italic);
        }
        if(through!=null && through){
            run.setStrikeThrough(through);
        }
        if(color!=null){
            run.setColor(color.toHEX());
        }
        if(bgColor!=null){
            //TODO
        }
        if(fontFamily!=null){
            run.setFontFamily(fontFamily);
        }
        if(fontSize != null){
            run.setFontSize(fontSize);
        }
    }

    @Override
    public List<DomNode> getChildren() {
        return null;
    }
}
